import Resources.Enums as Enums
from Classes import *


def DivideClause(tokenStrList):
    ret = []
    for tokenStr in tokenStrList:
        sentence = Sentence()
        clause = Clause()

        # devide
        for token in tokenStr:
            if ((token.kind == Enums.Kind.PUNC) | (token.kind == Enums.Kind.GRAMMER)) & (clause.len != 0):
                sentence.append(clause)
                if token.kind == Enums.Kind.GRAMMER:
                    clause = Clause()
                    clause.append(token)
                else:
                    clause = Clause()
            else:
                clause.append(token)
        if clause.len != 0:
            sentence.append(clause)

        # re-merge
        senLen = sentence.len
        removeLocs = []
        for i in range(senLen):
            clause = sentence.get(i)
            if (clause.len == 1) & (clause.get(0).kind == Enums.Kind.OBJECT) & (i < senLen - 1):
                sentence.get(i + 1).insert(0, clause.get(0))
                removeLocs.append(i)
        for loc in removeLocs:
            sentence.pop(loc)

        ret.append(sentence)
    return ret
